package love.yuanmengrobot.wuziqi.UIzjj;/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:zjj
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/5/11 19:52
 */


import love.yuanmengrobot.wuziqi.Util.Point;
import love.yuanmengrobot.wuziqi.Util.PontGroup;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Judge_score {
    public static int ROWS = 15;//棋盘行数
    public static int COLS = 15;//棋盘列数

    public static void main(String[] args) {

    }

    public Map<Color,Integer> getScore(ArrayList<love.yuanmengrobot.wuziqi.Util.Point> chessList){

        Map<Color, Integer> colorIntegerMap = new HashMap<Color, Integer>();
        ArrayList<PontGroup> pontGroups = new ArrayList<PontGroup>();
        ArrayList<love.yuanmengrobot.wuziqi.Util.Point> points = new ArrayList<love.yuanmengrobot.wuziqi.Util.Point>();
        points.addAll(chessList);
        Judge_score.quickSort(points);
        for(love.yuanmengrobot.wuziqi.Util.Point point:points){

            //横向
            int dirction=1;
            PontGroup pontGroup=getPontGroup(point,pontGroups,dirction);
            if(pontGroup==null){
                pontGroup= new PontGroup();
                pontGroup.setDirection(dirction);
                pontGroup.addPoint(point);
                pontGroups.add(pontGroup);
            }


            //右
            if(pontGroup.getArr().size()<2){          //如果左边已经有了同颜色的说明横向的一排已经全部加入组里了，就不需要往右搜索了
                for(int x=point.getX()+1;x<=ROWS;x++){//横向向右寻找
                    love.yuanmengrobot.wuziqi.Util.Point chess = getChess(x, point.getY(), point.getColor(),chessList);
                    if(chess!=null){
                        mergePont(point,chess,pontGroups,dirction);
                    }else
                        break;
                }
            }

            //左斜
            dirction=2;
            PontGroup pontGroup2=getPontGroup(point,pontGroups,dirction);
            if(pontGroup2==null){
                pontGroup2= new PontGroup();
                pontGroup2.setDirection(dirction);
                pontGroup2.addPoint(point);
                pontGroups.add(pontGroup2);
            }
            //右下斜

            if(pontGroup2.getArr().size()<2){
                for(int x=point.getX()+1,y=point.getY()+1;x<=ROWS&&y<=ROWS;x++,y++){
                    love.yuanmengrobot.wuziqi.Util.Point chess = getChess(x, y, point.getColor(),chessList);
                    if(chess!=null){
                            mergePont(point,chess,pontGroups,dirction);
                    }else
                        break;
                }
            }


            //垂直
            dirction=3;
            PontGroup pontGroup3=getPontGroup(point,pontGroups,dirction);
            if(pontGroup3==null){
                pontGroup3= new PontGroup();
                pontGroup3.setDirection(dirction);
                pontGroup3.addPoint(point);
                pontGroups.add(pontGroup3);
            }

            //下
            if(pontGroup3.getArr().size()<2){
                for(int y=point.getY()+1;y<=ROWS;y++){
                   love.yuanmengrobot.wuziqi.Util.Point chess = getChess(point.getX(), y, point.getColor(),chessList);
                    if(chess!=null){
                            mergePont(point,chess,pontGroups,dirction);
                    }else
                        break;
                }
            }

            dirction=4;
            PontGroup pontGroup4=getPontGroup(point,pontGroups,dirction);
            if(pontGroup4==null){
                pontGroup4= new PontGroup();
                pontGroup4.setDirection(dirction);
                pontGroup4.addPoint(point);
                pontGroups.add(pontGroup4);
            }
            //左下
            if(pontGroup4.getArr().size()<2){
                for(int y=point.getY()+1,x=point.getX()-1;y<=ROWS&&x>=0;y++,x--){
                    love.yuanmengrobot.wuziqi.Util.Point chess = getChess(x, y, point.getColor(),chessList);
                    if(chess!=null){
                            mergePont(point,chess,pontGroups,dirction);
                    }else
                        break;
                }
            }
        }


        for(PontGroup pontGroup:pontGroups){
            int size = pontGroup.getArr().size();
            if(size>1){
                int score=0;
                if(size<=5){
                    score=size*(size-1);
                }else {
                    score=20;
                }
                int a=colorIntegerMap.get(pontGroup.getArr().get(0).getColor())==null?0:colorIntegerMap.get(pontGroup.getArr().get(0).getColor());
                colorIntegerMap.put(pontGroup.getArr().get(0).getColor(),score+a);
            }
        }
        return colorIntegerMap;
    }





    //按左上到右下把棋子排好
    public static ArrayList<love.yuanmengrobot.wuziqi.Util.Point> quickSort(ArrayList<love.yuanmengrobot.wuziqi.Util.Point> arr) {
        love.yuanmengrobot.wuziqi.Util.Point pivot = arr.get(0);
        ArrayList<love.yuanmengrobot.wuziqi.Util.Point> smaller = new ArrayList<love.yuanmengrobot.wuziqi.Util.Point>();
        ArrayList<love.yuanmengrobot.wuziqi.Util.Point> bigger = new ArrayList<love.yuanmengrobot.wuziqi.Util.Point>();
        for(int i=1; i<arr.size();i++){
            if(pivot.getY()>arr.get(i).getY()||(pivot.getY()==arr.get(i).getY()&&pivot.getX()>arr.get(i).getX())){
                smaller.add(arr.get(i));
            }else {
                bigger.add(arr.get(i));
            }

        }

        if(smaller.size() > 1)
            quickSort(smaller);
        if(bigger.size() > 1)
            quickSort(bigger);
        arr.removeAll(arr);
        arr.addAll(smaller);
        arr.add(pivot);
        arr.addAll(bigger);
        return  arr;
    }



    //根据坐标和颜色寻找棋子
    private love.yuanmengrobot.wuziqi.Util.Point getChess(int xIndex, int yIndex, Color color, ArrayList<love.yuanmengrobot.wuziqi.Util.Point> chessList) {
        for (love.yuanmengrobot.wuziqi.Util.Point c : chessList) {
            if (c != null && c.getX() == xIndex && c.getY() == yIndex && c.getColor() == color)
                return c;
        }
        return null;
    }



    //合并组
    public void mergePont(love.yuanmengrobot.wuziqi.Util.Point point1, love.yuanmengrobot.wuziqi.Util.Point point2, ArrayList<PontGroup> pontGroups, int dirction){
        PontGroup pontGroup = getPontGroup(point2, pontGroups, dirction);
        PontGroup pontGroup1 = getPontGroup(point1, pontGroups, dirction);
        if(pontGroup==null){
            pontGroup1.addPoint(point2);
        }else {
            if(pontGroup.getArr().size()>pontGroup1.getArr().size()){
                pontGroup.getArr().addAll(pontGroup1.getArr());
                pontGroups.remove(pontGroup1);
            }else {
                pontGroup1.getArr().addAll(pontGroup.getArr());
                pontGroups.remove(pontGroup);
            }
        }

    }


    //获得某个点某个方向的组
    public PontGroup getPontGroup(Point point, ArrayList<PontGroup> pontGroups, int dirction){
        for(PontGroup pontGroup:pontGroups){
            if(pontGroup.getDirection()==dirction&&pontGroup.getArr().contains(point)){
                return pontGroup;
            }
        }
        return null;
    }

}
